package com.yeook.ymax.ui.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.yeook.ymax.R;
import com.yeook.ymax.app.ApplicationYMAX;
import com.yeook.ymax.db.Database;
import com.yeook.ymax.db.SimpleCursorLoader;
import com.yeook.ymax.ui.activitys.ImagePagerActivity;
import com.yeook.ymax.ui.activitys.RingtoneUIActivity;
import com.yeook.ymax.ui.entity.CateView;
import com.yeook.ymax.ui.entity.RingtoneView;
import com.yeook.ymax.ui.ringtone.RingMakerEditActivity;

@SuppressLint("NewApi")
public class RingtoneListFragment extends Fragment  implements LoaderCallbacks<Cursor> {
	RingtoneUIActivity activity = null;
	
	DisplayImageOptions options;
	
	int cateCode = -1;
	
	String title;
	
	private RingtoneListAdapter ringtoneListAdapter;
	
	private Database db = Database.getInstance();
	
	public RingtoneListFragment()
	{
		
	}
	
	public RingtoneListFragment(int _cateCode, String _title)
	{
		cateCode = _cateCode;
		title = _title;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		activity = (RingtoneUIActivity) getActivity();
		if (cateCode == -1 && savedInstanceState != null)
			cateCode = savedInstanceState.getInt("cateCode");
		
		activity.cateCode = cateCode;
		
		activity.setTitle(title);
		
		activity.ringtoneViews.clear();
		
		ringtoneListAdapter = new RingtoneListAdapter(activity, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		
		View gv = inflater.inflate(R.layout.ringtone_list, null);
		//XmlLoader xmlLoader = new XmlLoader();
		//activity.imageUrls = xmlLoader.getImageUrls(xmlLoader.praseXml());
		options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.ic_empty_small)
			.showImageForEmptyUri(R.drawable.ic_empty_small)
			.showImageOnFail(R.drawable.ic_error_small)
			.cacheInMemory().imageScaleType(ImageScaleType.EXACTLY)
			.cacheOnDisc()
			.bitmapConfig(Bitmap.Config.RGB_565)
			.build();

		activity.listView = (ListView) gv.findViewById(R.id.ringtone_list_data);
		activity.listView.setAdapter(ringtoneListAdapter);
		activity.ringtoneLoader = activity.getSupportLoaderManager().initLoader(0, null, this);
		activity.ringtoneLoader.forceLoad();
		AdView adView = (AdView)gv.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());
		return gv;
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("cateCode", activity.cateCode);
	}
	
	static class ViewHolder {
		TextView name;
		TextView cate;
		TextView tags;
		String url;
	}
	
	public class RingtoneListAdapter extends CursorAdapter {
		public RingtoneListAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ViewHolder holder = (ViewHolder) view.getTag();
			if (holder == null) {
				holder = new ViewHolder();
				holder.name = (TextView) view.findViewById(R.id.ringtone_name);
				holder.cate = (TextView) view.findViewById(R.id.ringtone_cate);
				holder.tags = (TextView) view.findViewById(R.id.ringtone_tags);
			}
			holder.url = ApplicationYMAX.DEFAULTWEBSERVIC + cursor.getString(5);
			holder.name.setText(cursor.getString(4));
			holder.tags.setText(cursor.getString(8));
			Integer cate = cursor.getInt(6);
			CateView[] cates = RingtoneMenuFragment.cates;
			for(CateView cv : cates)
			{
				if(cv.getCode() == cate)
				{
					holder.cate.setText(cv.getName());
				}
			}
			
			view.setTag(holder);
			
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					//Intent intent = new Intent(activity, RingMakerEditActivity.class);
					 Intent intent = new Intent(Intent.ACTION_EDIT,
                             Uri.parse("filename"));
					  intent.putExtra("was_get_content_intent",
					                  false);
					  Object tag = v.getTag();
					  if(tag != null && tag instanceof ViewHolder)
					  {
						  intent.putExtra("name", ((ViewHolder)tag).name.getText());
						  intent.putExtra("ringtoneUrl", ((ViewHolder)tag).url);
					  }
					  intent.setClassName(
						activity,
					      RingMakerEditActivity.class.getName());
					startActivity(intent);
				}
				
			});
			
			activity.ringtoneViews.add(new RingtoneView());
		}

		@Override
		public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
			View v = LayoutInflater.from(activity).inflate(R.layout.ringtone_item, null);
			Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in);
			v.startAnimation(animation);
			return v;
		}
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		SimpleCursorLoader a = new SimpleCursorLoader(activity) {

			@Override
			public Cursor loadInBackground() {
				return db.getRingtoneViews(activity.cateCode);
			}
			
		};
		return a;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
		ringtoneListAdapter.swapCursor(data);	
		if(data.getCount() == 0)
		{
			Toast toast= Toast.makeText(activity, activity.getString(R.string.loading_face_detection), Toast.LENGTH_SHORT);
			toast.show();
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		ringtoneListAdapter.swapCursor(null);
		activity.ringtoneViews.clear();
	}

}
